﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Configuration;
using DataCentral.Dataset;
using DataCentral.Dataset.DataCentralDatasetTableAdapters;
using CommonData;

namespace DataCentral.Forms
{
    public partial class DBConfigurationForm : CommonChildForm
    {
        private bool _IsEdit = false;
        private DataCentralDataset.DatabaseConfigurationRow _DRDatabase = null;
        private DataCentralDataset _DSCentral;
        private DatabaseConfigurationTableAdapter _DADatabase;
        private string connectionString = "";

        public DBConfigurationForm(bool isEdit, DataCentralDataset.DatabaseConfigurationRow drDatabase, DataCentralDataset dsCentral, DatabaseConfigurationTableAdapter daDatabase)
        {
            InitializeComponent();
            _IsEdit = isEdit;
            _DRDatabase = drDatabase;
            _DSCentral = dsCentral;
            _DADatabase = daDatabase;
        }

        void GetServerConnectionString()
        {
            connectionString = String.Format("data source={0}; initial catalog= {1}; integrated security=SSPI", txtServer.Text,cbbDatabase.Text);
            if (rdgConnect.SelectedIndex == 1)
                connectionString = String.Format("data source={0}; initial catalog= {1}; user id={2}; password={3}", txtServer.Text, cbbDatabase.Text, txtLogin.Text, txtPassword.Text);           
        }

        private void SQLConnectionForm_Load(object sender, EventArgs e)
        {
            CommonFunctions.CreateDatabaseNameList(cbbDatabase);
            if (_IsEdit && _DRDatabase != null)
            {
                txtConfigName.Text = _DRDatabase.ConfigName;
                txtServer.Text = _DRDatabase.Datasource;
                cbbDatabase.Text = _DRDatabase.InitCatalog;
                rdgConnect.SelectedIndex = _DRDatabase.IntegratedSecurity ? 0 : 1;
                txtLogin.Text = _DRDatabase.UserID;
                txtPassword.Text = CommonFunctions.DecodeFrom64(_DRDatabase.Password);
                txtConnectionString.Text = _DRDatabase.ConnectionString;
            }
            else
            {                
                txtConfigName.Text = "Cơ sở dữ liệu chính";
                txtServer.Text = Properties.Settings.Default.APP_Datasource;
                cbbDatabase.Text = Properties.Settings.Default.APP_InitCatalog;
                rdgConnect.SelectedIndex = Properties.Settings.Default.APP_IntegratedSecurity ? 0 : 1;
                txtLogin.Text = Properties.Settings.Default.APP_ServerID;
                txtPassword.Text = CommonFunctions.DecodeFrom64(Properties.Settings.Default.APP_ServerPassword);
            }
        }

        private void rdgConnect_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool disable = rdgConnect.SelectedIndex == 0;
            DisableSQLServerAuthentication(disable);
        }

        void DisableSQLServerAuthentication(bool disable)
        {
            txtLogin.Enabled = !disable;
            txtPassword.Enabled = !disable;

            if (disable)
            {
                txtLogin.Text = string.Empty;
                txtPassword.Text = string.Empty;
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            GetServerConnectionString();
            txtConnectionString.Text = connectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    MessageClass.ShowInformationMessage("Kết nối cơ sở dữ liệu thành công với thông tin được cấu hình.");
                }
                catch
                {
                    MessageClass.ShowErrorMessage("Kết nối cơ sở dữ liệu không thành công.");
                    return;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            GetServerConnectionString();
            if (_IsEdit)
            {
                _DRDatabase.ConfigName = txtConfigName.Text.Trim();
                _DRDatabase.Datasource = txtServer.Text.Trim();
                _DRDatabase.InitCatalog = cbbDatabase.Text.Trim();
                _DRDatabase.IntegratedSecurity = rdgConnect.SelectedIndex == 0;
                _DRDatabase.UserID = txtLogin.Text.Trim();
                _DRDatabase.Password = CommonFunctions.EncodeTo64(txtPassword.Text.Trim());
                _DRDatabase.ConnectionString = connectionString;
            }
            else
            {
                _DRDatabase = _DSCentral.DatabaseConfiguration.NewDatabaseConfigurationRow();
                _DRDatabase.ConfigName = txtConfigName.Text.Trim();
                _DRDatabase.Datasource = txtServer.Text.Trim();
                _DRDatabase.InitCatalog = cbbDatabase.Text.Trim();
                _DRDatabase.IntegratedSecurity = rdgConnect.SelectedIndex == 0;
                _DRDatabase.UserID = txtLogin.Text.Trim();
                _DRDatabase.Password = CommonFunctions.EncodeTo64(txtPassword.Text.Trim());
                _DRDatabase.ConnectionString = connectionString;
                _DSCentral.DatabaseConfiguration.AddDatabaseConfigurationRow(_DRDatabase);
            }
            _DADatabase.Update(_DSCentral.DatabaseConfiguration);
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}